import { defineStore } from 'pinia'
import request from '@/utils/request'
import type { ApiResponse } from '@/types/api'

// 定义律所接口
interface Lawfirm {
  id: number
  title: string
  provinceid: number
  cityid: number
  distid: number
  address: string
  thumb: string
  phone: string
  email: string
  create_time: number
  update_time: number
  // 扩展字段
  province?: string
  city?: string
  dist?: string
}

// 定义律师接口
interface Lawyer {
  id: number
  firmid: number
  lawfirm?: string
  lawfirm_name?: string
  name: string
  username: string
  nickname: string
  thumb: string
  sex: number
  mobile: string
  phone: string
  email: string
  provinceid: number
  cityid: number
  distid: number
  address: string
  idcard: string
  id_number: string
  id_photo: string
  lawyer_license: string
  education: string
  honor: string
  adept: string
  desc: string
  type: number
  status: number
  entry_time: number // 执业日期时间戳
  create_time: number
  update_time: number
  last_login_time: number
  last_login_ip: string
  login_num: number
  // 扩展字段
  province?: string
  city?: string
  dist?: string
}

interface LawyerState {
  lawfirms: Lawfirm[]
  lawfirmTotal: number
  lawyers: Lawyer[]
  lawyerTotal: number
}

// 律所管理相关API
const lawyerApi = {
  // 获取律所列表
  getLawfirmList: (params: any): Promise<ApiResponse<Lawfirm[]>> => {
    return request({
      url: '/api/lawyer/lawfirm/list',
      method: 'get',
      params,
    })
  },

  // 添加/编辑律所
  saveLawfirm: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/lawyer/lawfirm/add',
      method: 'post',
      data,
    })
  },

  // 删除律所
  deleteLawfirm: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/lawyer/lawfirm/delete',
      method: 'post',
      data,
    })
  },

  // 获取律师列表
  getLawyerList: (params: any): Promise<ApiResponse<Lawyer[]>> => {
    return request({
      url: '/api/lawyer/list',
      method: 'get',
      params,
    })
  },

  // 添加律师
  addLawyer: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/lawyer/add',
      method: 'post',
      data,
    })
  },

  // 编辑律师
  editLawyer: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/lawyer/edit',
      method: 'put',
      data,
    })
  },

  // 禁用/恢复律师账号
  setLawyerStatus: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/lawyer/set',
      method: 'post',
      data,
    })
  },

  // 重置律师密码
  resetLawyerPassword: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/user/reset_password',
      method: 'post',
      data,
    })
  },

  // 设置律师登录权限
  setLawyerLoginPermission: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/lawyer/lawyer/toggle_login_permission',
      method: 'post',
      data,
    })
  },

  // 同步律师到用户系统
  syncLawyerToUser: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/lawyer/sync/sync_lawyer_to_user',
      method: 'post',
      data,
    })
  },

  // 批量同步律师
  batchSyncLawyers: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/lawyer/sync/batch_sync',
      method: 'post',
      data,
    })
  },

  // 获取可同步的律师列表
  getSyncableLawyers: (params: any): Promise<ApiResponse<Lawyer[]>> => {
    return request({
      url: '/api/lawyer/sync/get_syncable_lawyers',
      method: 'get',
      params,
    })
  },

  // 获取律所律师列表（用于案件管理）
  getLawFirmLawyers: (params: any): Promise<ApiResponse<Lawyer[]>> => {
    return request({
      url: '/api/lawyer/lawyer/get_law_firm_lawyers',
      method: 'get',
      params,
    })
  },
}

export const useLawyerStore = defineStore('lawyer', {
  state: (): LawyerState => ({
    lawfirms: [],
    lawfirmTotal: 0,
    lawyers: [],
    lawyerTotal: 0,
  }),

  actions: {
    // 获取律所列表
    async getLawfirmListAction(params: any): Promise<ApiResponse<Lawfirm[]>> {
      try {
        const res = await lawyerApi.getLawfirmList(params)
        if (res.code === 0) {
          this.lawfirms = res.data
          this.lawfirmTotal = res.count || 0
        }
        return res
      } catch (error) {
        throw error
      }
    },

    // 添加/编辑律所
    async saveLawfirmAction(data: any) {
      try {
        const res = await lawyerApi.saveLawfirm(data)
        return res
      } catch (error) {
        throw error
      }
    },

    // 删除律所
    async deleteLawfirmAction(data: any) {
      try {
        const res = await lawyerApi.deleteLawfirm(data)
        return res
      } catch (error) {
        throw error
      }
    },

    // 获取律师列表
    async getLawyerListAction(params: any): Promise<ApiResponse<Lawyer[]>> {
      try {
        const res = await lawyerApi.getLawyerList(params)
        if (res.code === 0) {
          this.lawyers = res.data
          this.lawyerTotal = res.count || 0
        }
        return res
      } catch (error) {
        throw error
      }
    },

    // 添加律师
    async addLawyerAction(data: any) {
      try {
        const res = await lawyerApi.addLawyer(data)
        return res
      } catch (error) {
        throw error
      }
    },

    // 编辑律师
    async editLawyerAction(data: any) {
      try {
        const res = await lawyerApi.editLawyer(data)
        return res
      } catch (error) {
        throw error
      }
    },

    // 禁用/恢复律师账号
    async setLawyerStatusAction(data: any) {
      try {
        const res = await lawyerApi.setLawyerStatus(data)
        return res
      } catch (error) {
        throw error
      }
    },

    // 重置律师密码
    async resetLawyerPasswordAction(data: any) {
      try {
        const res = await lawyerApi.resetLawyerPassword(data)
        return res
      } catch (error) {
        throw error
      }
    },

    // 设置律师登录权限
    async setLawyerLoginPermissionAction(data: any) {
      try {
        const res = await lawyerApi.setLawyerLoginPermission(data)
        return res
      } catch (error) {
        throw error
      }
    },

    // 同步律师到用户系统
    async syncLawyerToUserAction(data: any) {
      try {
        const res = await lawyerApi.syncLawyerToUser(data)
        return res
      } catch (error) {
        throw error
      }
    },

    // 批量同步律师
    async batchSyncLawyersAction(data: any) {
      try {
        const res = await lawyerApi.batchSyncLawyers(data)
        return res
      } catch (error) {
        throw error
      }
    },

    // 获取可同步的律师列表
    async getSyncableLawyersAction(params: any) {
      try {
        const res = await lawyerApi.getSyncableLawyers(params)
        return res
      } catch (error) {
        throw error
      }
    },

    // 获取律所律师列表（用于案件管理）
    async getLawFirmLawyersAction(params: any) {
      try {
        const res = await lawyerApi.getLawFirmLawyers(params)
        return res
      } catch (error) {
        throw error
      }
    },
  },
})
